<?php

use Doraemon\service\Event\Event;
use Doraemon\service\Event\TaskRelatedEvent;
use Doraemon\tools\Log;

class DailyTaskAction extends ActionPlugin
{
    const IS_TRACING = true;

    /**
     * @throws Exception
     */
    public function main()
    {
        Log::info('日常任务接收到的订阅消息', $this->messageBody);

        if (!$this->messageBody['route_key']) throw new \Exception('未接收到route_key');
        if (!$this->messageBody['data']) throw new \Exception('未接收到数据');

        $isQuality = TaskRelatedEvent::isQuality($this->messageBody);
        if (!$isQuality) throw new \Exception('内容质量不符合当前任务');

        $subEventsIds = Event::getSubEventIdByRouteKey($this->messageBody['route_key']);
        if (!$subEventsIds['event_id'] || !$subEventsIds['hit_sub_event_ids']) throw new \Exception('未获取到订阅事件id');

        $subEvents = Event::getSubEvents($subEventsIds['hit_sub_event_ids']);
        if (!$subEvents) throw new \Exception('未获取到订阅事件详情');

        $taskIds = TaskRelatedEvent::getTaskIds($this->messageBody, $subEvents);
        Log::debug('任务id', $taskIds);
        TaskRelatedEvent::handleTask([
            'uid' => $taskIds['uid'] ?? 0,
            'event_id' => $subEventsIds['event_id'],
            'sub_event_task_ids' => $taskIds['hit_ids'] ?? [],
        ]);

        $this->setSuccess();
    }
}